$(document).ready(function(){
	//VS
	$('.win_btn').click(function(){
		//必要操作
		$('button').attr('disabled',true);
		$vote_succ = $('.vote_succ').clone();
		$vote_succ.removeClass('none');
		var index = $('.win_btn').index(this);
		$('.pic_wrap').eq(index).append($vote_succ);
		//关键参数
		var left_id = $('.userid').eq(0).text();
		var right_id = $('.userid').eq(1).text();
		var left_score = $('#left_score').val();
		var right_score = $('#right_score').val();
		//Ajax
		setTimeout(function(){
			$.post('ajax/getUser.php',{'left_id':left_id,'right_id':right_id,'left_score':left_score,'right_score':right_score,'awin':(1-index)},function(data){
				var info = data.split("|");
				$('.userid').eq(0).text(info[0]);
				$('#vote_left').css({'background':'url(girls/'+info[1]+') no-repeat','background-position':'50% 50%'});
				$('#left_score').val(info[2]);
				$('.userid').eq(1).text(info[3]);
				$('#vote_right').css({'background':'url(girls/'+info[4]+') no-repeat','background-position':'50% 50%'});
				$('#right_score').val(info[5]);
			})
		},300)
	})//end_VS
	
	//平手
	$('#draw_btn').click(function(){
		$vote_succ = $('.vote_succ').clone();
		$('#footer').after($vote_succ);
		$('button').attr('disabled',true);
		setTimeout(function(){
			$.post('ajax/getUser.php',{'status':'draw'},function(data){
				var info = data.split("|");
				$('.userid').eq(0).text(info[0]);
				$('#vote_left').css({'background':'url(girls/'+info[1]+') no-repeat','background-position':'50% 50%'});
				$('#left_score').val(info[2]);
				$('.userid').eq(1).text(info[3]);
				$('#vote_right').css({'background':'url(girls/'+info[4]+') no-repeat','background-position':'50% 50%'});
				$('#right_score').val(info[5]);
			})
		},300)
	})//end_draw
	
	//Ajax Start and Stop
	$(document).ajaxStart(function(){
		$('.vote_succ').eq(0).remove();
		$('.userid').eq(0).text('loading...');
		$('#vote_left').css({'background':'url(templates/images/ajax-loader.gif) no-repeat','background-position':'50% 50%'});
		$('.userid').eq(1).text('loading...');
		$('#vote_right').css({'background':'url(templates/images/ajax-loader.gif) no-repeat','background-position':'50% 50%'});
	}).ajaxStop(function(){
		$('button').attr('disabled',false);
		$('input:submit').attr('disabled',false);
	})
	
	//生成提示框
	var $alert_board = $('<div class="alert_board w h tc none"><div class="alert_bg pa h w"></div><div class="tips_board w500 tl h300 oh dib vm pr"><div class="exclamation h300 png vm dib"></div><div class="alert_msg dib vm f24"></div></div><span class="vm dib h verticalMiddle"></span></div>');
	
	//举报提交
	$('.report_sub').click(function(){
		var report_id = $('#report_id').val();
		
		if(report_id == ''){
			$('body').append($alert_board);
			var windH = $(window).height();
			$('.alert_tips_wrap').height(windH);
			$('html').css('overflow','hidden');
			$('.alert_msg').html('打假报告，罚做俯卧撑100下!!!');
			$('.alert_board').show();
		}else{
			if(isNaN(report_id)){
				$('body').append($alert_board);
				var windH = $(window).height();
				$('.alert_tips_wrap').height(windH);
				$('html').css('overflow','hidden');
				$('.alert_msg').html('ID号是纯数字啦!!!不要以为我不知道你输入了什么东东，哼哼');
				$('.alert_board').show();
			}else{
				$.post('ajax/getUser.php',{'report_id':report_id},function(data){
					if(data == 0){
						$('body').append($alert_board);
						var windH = $(window).height();
						$('.alert_tips_wrap').height(windH);
						$('html').css('overflow','hidden');
						$('.alert_msg').html('哼哼，根本就没有这个人，还想骗我!!!');
						$('.alert_board').show();
					}else{
						$('body').append($alert_board);
						var windH = $(window).height();
						$('.alert_tips_wrap').height(windH);
						$('html').css('overflow','hidden');
						$('.alert_msg').html('提交成功，等我的好消息吧！');
						$('.alert_board').show();
						setTimeout(function(){location.href="index"},1000);
					}
				})
			}
		}
		$('.alert_bg').click(function(){
			$('.alert_board').hide();
			$('#report_id').val('');
			$('#report_id').focus();
			$('html').css('overflow','visible');
		})
		return false;
	})//end_report
	
	
	//返回顶部
	var back_top = '<div class="back_top none"><img src="templates/images/back_top.png" title="返回顶部"></div>';
	$(".back_top").remove();//防止多次加载
	$("body").append(back_top);
	$(".back_top").css({position:"fixed",right:"5px",bottom:"5px"});
	$(".back_top").live('mouseover',function(){
		$(".back_top").css({opacity:".8"});
	});
	$(".back_top").live('mouseout',function(){
		$(".back_top").css({opacity:"1"});
	});
	$(window).scroll(function(){
		if($(window).scrollTop() > $(window).height()){
			$(".back_top").show();
		}else{
			$(".back_top").hide();
		}
	});
	$(".back_top").live('click',function(){
		$("body,html").animate({scrollTop:0},100);
	});
	
	//快捷键
	$('html').keydown(function(e){
		if(e.keyCode == 37){
			$('.win_btn').eq(0).click();
		}else if(e.keyCode == 39){
			$('.win_btn').eq(1).click();
		}else if(e.keyCode == 40){
			$('#draw_btn').click();
			event.returnValue=false;
		}
	})
	
})